package com.zhuiyun.project.api.friend.mapper;

import com.zhuiyun.project.api.friend.entity.Friend;
import com.zhuiyun.project.api.friend.model.FriendModel;
import com.zhuiyun.project.api.messagelist.entity.MessageList;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * @ClassName FriendMapper
 * @Description TODO 好友 mapper
 * @Author kdj
 * @Date 2023/6/20 14:18
 **/
@Mapper
public interface FriendMapper extends BaseMapper<Friend> {
    /**
     * @param fromId
     * @param toId
     * @Author kdj
     * @Description 删除好友
     * @Date 2023/7/21 14:36
     * @Return int
     */
    @Update(" UPDATE friend" +
            "            SET del_flag=1 " +
            "            WHERE del_flag=0" +
            "            AND ((user_id = #{fromId} AND friend_id =  #{toId})" +
            "            OR(user_id =  #{toId} AND friend_id = #{fromId}))")
    int delFriend(Integer fromId, Integer toId);

    /**
     * @param userId
     * @param friendId
     * @Author kdj
     * @Description 查询是否还是好友？
     * @Date 2023/7/21 16:06
     * @Return java.util.List<com.zhuiyun.project.api.friend.entity.Friend>
     */
    @Select("SELECT * FROM friend WHERE del_flag = 0  and ( ( user_id = #{userId} and friend_id = #{friendId} ) " +
            "or ( user_id = #{friendId} and friend_id = #{userId} ) )")
    List<Friend> queryIsFriend(int userId, int friendId);


    /**
     * 单聊消息免打扰功能
     * @author 温
     * @date 2023/7/23 19:39
     * @parameter [isMuted, fromId, toId]
     * @return int
     */
    @Update("update friend set is_muted=#{isMuted} WHERE friend_id=#{fromId} and user_id=#{toId};")
    int updateMeted( Integer isMuted,Integer fromId,Integer toId);
    @Update("update message_list set is_muted=#{isMuted} WHERE from_id=#{fromId} and to_id=#{toId};" )
    int updateMeted1( Integer isMuted,Integer fromId,Integer toId );


//    /**
//     * 群聊消息免打扰
//     * @author 温
//     * @date 2023/7/31 8:57
//     * @parameter
//     * @return
//     */
//    @Update("update friend set is_muted=#{isMuted} WHERE friend_id=#{fromId} and user_id=#{toId};")
//    int updateIsMeted( Integer isMuted,Integer fromId,Integer toId);
//    @Update("update group_message set is_muted=#{isMuted} WHERE from_id=#{fromId} and to_id=#{toId};" )
//    int updateIsMeteds( Integer isMuted,Integer fromId,Integer toId);


    /**
     * 修改好友备注
     * @author 温
     * @date 2023/7/24 10:48
     * @parameter [friend]
     * @return int
     */
    @Update("update friend set remark_name=#{remarkName} WHERE friend_id=#{fromId} and user_id=#{toId};")
    int friendUpdate(String remarkName,Integer fromId,Integer toId);

    /**
     * 单查列表信息
     * @author 温
     * @date 2023/7/25 10:44
     * @parameter [messageList]
     * @return com.zhuiyun.project.api.messagelist.entity.MessageList
     */
    @Select("select * from message_list where del_flag=0 and from_id=#{fromId} and to_id=#{toId} ")
    MessageList selectOneMessageList(MessageList messageList);

/**
 * 获取好友列表
 * @author 温
 * @date 2023/7/27 20:56
 * @parameter [userId]
 * @return java.util.List<com.zhuiyun.project.api.friend.model.FriendModel>
 */

    @Select("SELECT f.friend_id, f.remark_name, u.image_id, u.user_name FROM friend f, app_user u WHERE f.friend_id = u.id AND f.user_id = #{userId} AND f.del_flag = 0 AND (f.remark_name LIKE CONCAT('%', #{remarkName}, '%') or u.user_name LIKE CONCAT('%', #{remarkName}, '%'))")
    List<FriendModel> selectFriend(@Param("userId") Integer userId, @Param("remarkName") String remarkName);


    /**
     *获取群聊列表信息
     * @author 温
     * @date 2023/8/1 15:47
     * @parameter
     * @return
     */
    @Select("<script>   " +
            " select aa.user_name,aa.image_id,bb.group_name,bb.group_num,bb.group_img_id,j.*\n" +
            " from app_user aa, group_chat bb,join_group j\n" +
             "where aa.id=2 and j.group_id=bb.id and j.user_id=#{userId} and  j.user_type=1 and bb.del_flag=0" +
             "<when test='groupName!=&quot;&quot; and groupName!=null'> " +
            "and bb.group_name like concat('%',#{groupName},'%') </when>" +
             "</script>")
    List<FriendModel>quancha(@Param("userId") Integer userId, @Param("groupName") String groupName ,@Param("groupId") Integer groupId);

    /**
     * @param id
     * @Author kdj
     * @Description 删除所有好友
     * @Date 2023/7/21 14:36
     * @Return int
     */
    @Update(" UPDATE friend" +
            "            SET del_flag=1 " +
            "            WHERE del_flag=0 AND user_id=#{id} OR friend_id=#{id} ")
    int delAllFriend(Integer id);
}
